package com.tz.fdz_jiance;

import com.tz.fdz_jiance.domain.Log;
import com.tz.fdz_jiance.mapper.LogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.time.LocalDateTime;


@Component
public class Porter  implements CommandLineRunner {

    @Autowired
    LogMapper logMapper;


    public Porter() throws IOException
    {
        serverSocket = new ServerSocket(9000);
        serverSocket.setSoTimeout(10000);
    }
    private ServerSocket serverSocket;
    @Override
    public void run(String... args) throws Exception {
        while(true)
        {
            try
            {
                System.out.println("等待远程连接，端口号为：" + serverSocket.getLocalPort() + "...");
                serverSocket.setSoTimeout(36000);

                Socket client = serverSocket.accept();
                System.out.println("远程主机地址：" + client.getRemoteSocketAddress());
                RecvThread recvThread = new RecvThread(client);
                recvThread.run();
            }catch(SocketTimeoutException s)
            {
                System.out.println(s);
            }catch(IOException e)
            {
                e.printStackTrace();
                break;
            }
        }
    }

    class RecvThread extends Thread {

        public RecvThread (Socket client) throws IOException {
            clientSocket = client;
            System.out.println("clientSocket newed");
        }

        private Socket clientSocket;
        @Override
        public void run(){
            try {
                InputStream inputStream = clientSocket.getInputStream();
                String messages = "";
                byte[] buffers = new byte[60];
                while (true){
                    inputStream.read(buffers);
                    String line = printbytes(buffers);
                    System.out.print( LocalDateTime.now() +"  :"+line );
                    posttoServer(line);
                    buffers= new byte[60];
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public String printbytes (byte[] bytes){
            String result = "";
            for (int i =0 ;i < bytes.length;i++){
                if(bytes[i] == 0){
                    break;
                }
                result += (char) bytes[i];
            }
            return  result;
        }
        public void posttoServer (String log) throws IOException {
            Log log1 = new Log();
            log = log.trim();
            String[] strings = log.split(":");

            if(strings.length >=5)
            {

                log1.setCreatetime(LocalDateTime.now());
                log1.setSpeed(Integer.parseInt(strings[0]) * 16*16 + Integer.parseInt(strings[1]) );
                log1.setDirection(Integer.parseInt(strings[2]) * 16*16 + Integer.parseInt(strings[3]) );
                log1.setSpeed(Integer.parseInt(strings[4]) );
                logMapper.insert(log1);
            }
        }
        public boolean hasinvalichar (String str){

            String  spec = "0123456789abcdef";
            str = str.toLowerCase();
            for(int i=0; i < str.length();i++){
                if(spec.contains(str.substring(i,i+1))){

                }else{
                    return true;
                }
            }
            return false;
        }
        public int getint(String cc) {
            cc = cc.toLowerCase();
            char [] spec = {'a','b','c','d','e','f'};
            int sum = 0;
            if (cc.length() == 2) {
                for(int i =0 ; i< spec.length ; i ++){
                    if(cc.charAt(0) == spec[i]){
                        sum += 16 * 10 * (i+1);
                    }
                }
                if(sum == 0){
                    sum += 16 * 10 * (Integer.parseInt(cc.substring(0,1)));
                }
            }
            return sum;
        }


    }

}
